import { useRequest } from 'umi';
import { message } from 'antd'
//基础服务接口
export interface BaseService<T> {
    queryList: (options?: { [key: string]: any }) => Promise<{ data: T[] }>;
    deleteOne: (id: number) => Promise<boolean>;
    updateOne: (entity: T) => Promise<boolean>;
    save: (entity: T) => Promise<boolean>;
}
export interface Wrapper<T> { }
//统一的BaseModel
export function BaseModel<T>(params: { pageSize: number; current: number }, service: BaseService<T>) {
    // debugger
    // const msg = useRequest(() => service.queryList(params));
    const delById = async (id = 0) => {
        try {
            await service.deleteOne(id as number);
            message.success('success');/*  */
            // msg.run();
        } catch (error) {
            message.error('fail');
        }
    };
    const modifyOne = async (entity: T) => {
        try {
            await service.updateOne(entity);
            message.success('success');
            // msg.run();
        } catch (error) {
            message.error('fail');
        }
    }
    const saveOne = async (entity: T) => {
        try {
            await service.save(entity);
            message.success('success');
            // msg.run();
        } catch (error) {
            message.error('fail');
        }
    }
    const query = async (entity: Partial<T>) => {
        try {
            let retData = await service.queryList(entity);
            // message.success('success');
            // msg.run();
            return retData.data;
        } catch (error) {
            console.log(error);
            message.error('fail');
        }
    }
    return {
        // dataSource: null,
        reload: ()=>{return query({})},
        // loading: msg.loading,
        deleteById: delById,
        updateOne: modifyOne,
        saveOne: saveOne,
        query: query
    };
}